package cn.wen233.sunglow.controllers;

import cn.wen233.basic.http.RestResponse;
import cn.wen233.core.ServiceManager;
import cn.wen233.core.filter.ServiceFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Service Api
 *
 * @author wenei
 * @date 2021-10-23 17:11
 */
@RestController
@RequestMapping("/services")
@RequiredArgsConstructor
public class SerivceApi {

    private final ServiceManager serviceManager;

    @PostMapping("/page")
    public RestResponse<?> page(@RequestBody ServiceFilter filter) {
            return RestResponse.success(serviceManager.findPageService(filter));
    }

    @GetMapping("/{namespaceId}")
    public RestResponse<?> getAll(@PathVariable String namespaceId) {
        return RestResponse.success(serviceManager.getAllService(namespaceId));
    }

    @GetMapping("/{namespaceId}/{group}")
    public RestResponse<?> getAllByGroup(@PathVariable String namespaceId, @PathVariable String group, String serviceName) {
        return RestResponse.success(serviceManager.getByNamespaceAndGroupAndServiceName(namespaceId, group, serviceName));
    }

    @PostMapping
    public RestResponse<?> create(String name, String appName, String groupName, String namespaceId) {
        serviceManager.createService(name, appName, groupName, namespaceId);
        return RestResponse.success();
    }

    @PutMapping("/{id}")
    public RestResponse<?> update(@PathVariable String id, String name, String appName, String groupName) {
        serviceManager.updateService(id, name, appName, groupName);
        return RestResponse.success();
    }

    @DeleteMapping("/{id}")
    public RestResponse<?> delete(@PathVariable String id) {
        serviceManager.deleteService(id);
        return RestResponse.success();
    }
}
